<?php
/**
 *Telegram @cocolive9 2023/9/25 16:12
 */


namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Config;
use think\Db;
use think\Validate;

/**
 * 会员接口
 */
class Wxpay extends Api
{

//    protected $noNeedRight = ['*'];
    protected $noNeedLogin = ['*'];

    public function _initialize()
    {

        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    /**
     * 微信支付
     */
    public function pays()
    {

        $appid = 'wxcd87d96da1327c80';

        $mch_id = '1617375128';
        $key = 'D1qJMTjUvsOQRetbmKyCnI4BEaGV2Wrg'; //32位字符

        $nonce_str = 'cz' . date('Ymdhis') . rand(100, 999);
        $zfmoney = 1;

        //注意 这里应该要保存下信息  方便回调验证
        $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';

        $parameters = array(
            'appid' => $appid, //服务商ID
            'mch_id' => $mch_id, //商户号
            'nonce_str' => $nonce_str, //随机字符串
            'body' => '充值',
            'out_trade_no' => $nonce_str,
            'total_fee' => $zfmoney * 100, //分
            'spbill_create_ip' => $_SERVER['REMOTE_ADDR'], //终端IP
            'notify_url' => 'http://' . $_SERVER['HTTP_HOST'] . '/api/wxpay/paybackc',  //回调
            'openid' => 'o-ws35Tkd00R9Ld4qCJCks5l_ln8', //用户openid
            'trade_type' => 'JSAPI'//交易类型
        );


        //统一下单签名
        $parameters['sign'] = getSign($parameters, $key);
        $xmlData = arrayToXml($parameters);
        $return = xmlToArray(postXmlCurl($xmlData, $url, 60));

        if (!isset($return['result_code'])){
            $this->error($return['return_msg']);
        }

        $parameter = array(
            'appId' => $appid, //小程序ID
            'timeStamp' => '' . time() . '', //时间戳
            'nonceStr' => $nonce_str, //随机串
            'package' => 'prepay_id=' . $return['prepay_id'], //数据包
            'signType' => 'MD5'//签名方式
        );
        //签名
        $parameter['paySign'] = getSign($parameter, $key);


        $this->success('支付信息', $parameter);


    }

    //退款
    public function paybackc(){

        $input=file_get_contents("php://input");

        $obj = simplexml_load_string($input, 'SimpleXMLElement', LIBXML_NOCDATA);

        $data = json_decode(json_encode($obj), true);


        $payorder=$data['out_trade_no'];





    }




}